package db

import (
	"fmt"

	"gitee.com/larry_wan/go-lib/utils"
	"github.com/jinzhu/gorm"
	_ "github.com/mattn/go-sqlite3"
)

var SQLite *gorm.DB

func Init() (err error) {
    dbFile := utils.DBFile()
	dbClientUrl := fmt.Sprintf("%s?loc=Asia/Shanghai", dbFile)

	SQLite, err = gorm.Open("sqlite3", dbClientUrl)
	if err != nil {
		return
	}

	SQLite.DB().SetMaxOpenConns(10)
	SQLite.SetLogger(utils.DefaultGormLogger)
	SQLite.LogMode(false)

	return
}

func Close() {
    if SQLite != nil {
		SQLite.Close()
		SQLite = nil
	}
}

